// 邮箱相关api
// @author ishot

package api

import (
	"admin-go-api/config"
	"admin-go-api/constant"
	"admin-go-api/core"
	"admin-go-api/global"
	"admin-go-api/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"gopkg.in/gomail.v2"
	"math/rand"
	"time"
)

// SendMail Captcha qq邮箱注册验证码
// @Summary qq邮箱注册验证码
// @Tags qq邮箱注册验证码相关接口
// @Produce json
// @Description qq邮箱注册验证码
// @Param data body model.QQMailDto true "data"
// @Success 200 {object} result.Result
// @Router /api/qqMail [post]
func SendMail(c *gin.Context) {
	// 发送邮箱业务
	var dto model.QQMailDto
	_ = c.BindJSON(&dto)
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	emailCode := fmt.Sprintf("%06v", rnd.Int31n(1000000))
	core.RedisDb.Set(global.Ctx, constant.EmailCode+dto.Email, emailCode, time.Minute*5)
	now := time.Now()
	t := fmt.Sprintf("%02d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
	html := fmt.Sprintf(`<div>
		<div>
			尊敬的%s, 您好!
		</div>
		<div style="padding: 8px 40px 8px 50px;">
			<p>您于 %s 提交的邮箱验证， 本次验证码为 %s ， 为了保证账号安全， 验证码有效时间为5分钟， 请确认为本人操作， 切勿向他人泄露， 感谢你的理解与使用，</p>
		</div>
		<div>
			<p>此邮箱为系统邮箱，请勿回复，</p>
		</div>
	</div>`, dto.Email, t, emailCode)
	// 组装数据
	subjectRedis, err := core.RedisDb.Get(global.Ctx, constant.EmailSubjectKey).Result()
	if err != nil {
		var sysConfig model.SysConfig
		core.RedisDb.Set(global.Ctx, sysConfig.ConfigKey, sysConfig.ConfigValue, 0)
		global.Log.Infof("从mysql中获取的邮箱主题为: %s", sysConfig.ConfigValue)
		msg := gomail.NewMessage()
		msg.SetHeaders("From", config.Config.Mail.Username)
		msg.SetHeaders("To", dto.Email)
		msg.SetHeaders("Subject", sysConfig.ConfigValue)
		msg.SetBody("text/html", html)
		dialer

		// msg := mail.Address{}

		//msg.
	}
}
